package com.hcrazy.manchester.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.hcrazy.manchester.app.pojo.Admin;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.services.AdminService;
import com.hcrazy.manchester.app.services.ForumRuleService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
//@SessionAttributes({"validateCode","admin"})
@SessionAttributes("admin")
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	
	@Autowired
	private ForumRuleService ruleService;
	
	@RequestMapping(value="/admin",method=RequestMethod.GET)
	public void admin(){
		
	}
	
	@RequestMapping(value="/admin/get/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Admin get(@PathVariable("id") int id){
		try {
			return adminService.getAdmin(id);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/*@RequestMapping(value="/login",method=RequestMethod.POST)
	public @ResponseBody int login(@ModelAttribute("form") Admin admin,
			@ModelAttribute("validateCode") String sessionValidateCode, 
			@RequestParam String validateCode,SessionStatus status,
			ModelMap map){
		int result = 0;
		//验证码判�?
		System.out.println("sessionValidateCode:"+sessionValidateCode);
		System.out.println("validateCode:"+validateCode);
		if(!validateCode.equalsIgnoreCase(sessionValidateCode)){
			return Constant.LOGIN_RESULT_VALIDATECODE_FAILED;
		}
		try {
			result = adminService.login(admin);
			admin.setAdminEmail(admin.getAdminName());
			admin = adminService.getAdminByUserNameOrEmail(admin);
			map.addAttribute("admin",admin);
			
			
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		return result;
	}*/
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public @ResponseBody int login(@ModelAttribute("form") Admin admin,ModelMap map){
		int result = 0;
	
		try {
			result = adminService.login(admin);
			admin.setAdminEmail(admin.getAdminName());
			admin = adminService.getAdminByUserNameOrEmail(admin);
			map.addAttribute("admin",admin);
			
			
			
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		return result;
	}
	
	@RequestMapping(value="/changePass",method=RequestMethod.GET)
	public void toPagechangePass(){
		
	}
	
	@RequestMapping(value="/changePass",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public String changePass(
			@RequestParam String adminPass,
			@RequestParam String adminNewPass,
			Model model,
			@ModelAttribute("admin") Admin admin){

		try {
			ErrorMsg msg = adminService.changePassword(admin, adminPass, adminNewPass);
			if(msg.getCode() != Constant.SUCCESS){
				model.addAttribute("message", msg.getMsg());
				return "/admin/failed";
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/changePass";
	}
}
